package de.lmu.mvs.lerngruppenfinder;

import com.eclipsesource.json.JsonObject;

import de.lmu.mvs.lerngruppenfinder.utils.BaseActivity;
import de.lmu.mvs.lerngruppenfinder.utils.ServerConnector;
import de.lmu.mvs.lerngruppenfinder.utils.ServerConnector.ServerConnectorListener;
import de.lmu.mvs.lerngruppenfinder.utils.ServerConnector.ServerError;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class PasswordResetActivity extends BaseActivity {

	// Values for email and password at the time of the login attempt.
	private String mEmail;

	// UI references.
	private EditText mEmailView;
	private View mResetFormView;
	private View mResetStatusView;
		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB){
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
		
		setContentView(R.layout.activity_password_reset);
		
		mEmailView = (EditText) findViewById(R.id.email);
		
		mResetFormView = findViewById(R.id.reset_form);
		mResetStatusView = findViewById(R.id.reset_status);
		
	}
	
	public void resetPassword(View view) {
		
		// Store values
		mEmail = mEmailView.getText().toString();
		
		// Fortschrittsanzeige
		showProgress(true);
		
		ServerConnector.resetPassword(new ServerConnectorListener<JsonObject>() {

			@Override
			public void requestFinished(JsonObject json) {
				//Log.d("pwreset", "finished");
				Toast toast = Toast.makeText(getApplicationContext(), "Neues Passwort wurde versendet", Toast.LENGTH_LONG);
				toast.show();
				
				// Fortschrittsanzeige beenden
				showProgress(false);
			}

			@Override
			public void requestError(ServerError error) {
				Toast toast = Toast.makeText(getApplicationContext(), "Es ist ein Fehler aufgetreten", Toast.LENGTH_LONG);
				toast.show();
				
				// Fortschrittsanzeige beenden
				showProgress(false);
			}
			
		}, mEmail);
	}
	
	/**
	 * Shows the progress UI and hides the reset form.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
	private void showProgress(final boolean show) {
		// On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
		// for very easy animations. If available, use these APIs to fade-in
		// the progress spinner.
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
			int shortAnimTime = getResources().getInteger(
					android.R.integer.config_shortAnimTime);

			mResetStatusView.setVisibility(View.VISIBLE);
			mResetStatusView.animate().setDuration(shortAnimTime)
					.alpha(show ? 1 : 0)
					.setListener(new AnimatorListenerAdapter() {
						@Override
						public void onAnimationEnd(Animator animation) {
							mResetStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
						}
					});

			mResetFormView.setVisibility(View.VISIBLE);
			mResetFormView.animate().setDuration(shortAnimTime)
					.alpha(show ? 0 : 1)
					.setListener(new AnimatorListenerAdapter() {
						@Override
						public void onAnimationEnd(Animator animation) {
							mResetFormView.setVisibility(show ? View.GONE : View.VISIBLE);
						}
					});
		} else {
			// The ViewPropertyAnimator APIs are not available, so simply show
			// and hide the relevant UI components.
			mResetStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
			mResetFormView.setVisibility(show ? View.GONE : View.VISIBLE);
		}
		
		if(show==false){
			finish();
		}
	}
}
